package com.xhu.collegestudentrentalsystem.module.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.xhu.collegestudentrentalsystem.module.user.dao.*;
import com.xhu.collegestudentrentalsystem.module.user.entity.*;
import com.xhu.collegestudentrentalsystem.module.user.entity.dto.HouseDTO;
import com.xhu.collegestudentrentalsystem.module.user.service.IFeedbackService;
import com.xhu.collegestudentrentalsystem.util.constant.EntityProperty;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhu huilin
 * @date 2023/3/29 13:17
 */
@Service
public class FeedbackServiceImpl implements IFeedbackService {

    @Autowired
    private FeedbackDAO feedbackDAO;

    @Autowired
    private HouseScoreDAO houseScoreDAO;

    @Autowired
    private HouseOrderDAO houseOrderDAO;

    @Autowired
    private HouseDAO houseDAO;

    @Autowired
    private UserDAO userDAO;

    /**
     * 用户提价反馈信息，向数据库表中插入一条记录
     */
    @Override
    public Integer submitFeedbackInfo(Feedback feedback) {
        return feedbackDAO.insert(feedback);
    }

    /**
     * 查询反馈信息列表，根据时间排序
     */
    @Override
    public List<Feedback> findCustomerFeedbackList(Long userId, Page page) {
        PageHelper.startPage(page.getCurrent(), page.getLimit());
        /*
            学生租客：查询自己的反馈信息
            房东：查询自己所有房子的反馈信息
         */
         new QueryWrapper<>();
        List<Feedback> feedbacks = new ArrayList<>();
        Integer count = 0;
        User user = userDAO.selectOne(new QueryWrapper<User>().eq(EntityProperty.ID, userId));
        if(EntityProperty.userRole.CUSTOMER.equals(user.getRole())){
            QueryWrapper<Feedback> qw =
                    new QueryWrapper<Feedback>()
                            .eq(EntityProperty.feedback.USER_ID, user.getId())
                            .orderByDesc(EntityProperty.CREATE_TIME);
            feedbacks = feedbackDAO.selectList(qw);
            count = feedbackDAO.selectCount(qw);
        }else if(EntityProperty.userRole.OWNER.equals(user.getRole())){
            feedbacks = feedbackDAO.findFeedbackByOwnerId(user.getId());
            count = feedbackDAO.findFeedbackCountByOwnerId(user.getId());
        }
        // 遍历填充订单信息
        for (Feedback feedback : feedbacks){
            HouseOrder houseOrder = houseOrderDAO.selectOne(new QueryWrapper<HouseOrder>().eq(EntityProperty.ID, feedback.getOrderId()));
            House house = houseDAO.selectOne(new QueryWrapper<House>().eq(EntityProperty.ID, houseOrder.getHouseId()));
            houseOrder.setHouse(house);
            feedback.setHouseOrder(houseOrder);
        }

        /*QueryWrapper<Feedback> qw = new QueryWrapper<Feedback>()
                .eq(EntityProperty.feedback.USER_ID, userId).orderByDesc(EntityProperty.CREATE_TIME);*/
        // List<Feedback> feedbacks = feedbackDAO.selectList(qw);
        // Integer count = feedbackDAO.selectCount(qw);
        page.setRows(count);
        return feedbacks;
    }

    /**
     * 提交评分信息
     */
    @Override
    public Integer submitScoreInfo(HouseScore houseScore) {
        // 总体分数 = 4个评分的平均值
        Double overall = (houseScore.getEnvironment() + houseScore.getConvenience() + houseScore.getFeel() + houseScore.getRecommend()) / 4;
        houseScore.setOverall(overall);
        return houseScoreDAO.insert(houseScore);
    }

    /**
     * 查询用户评分信息
     */
    @Override
    public HouseScore findHouseScore(Long orderId, Long userId) {
        return houseScoreDAO.selectOne(
                new QueryWrapper<HouseScore>()
                        .eq(EntityProperty.feedback.ORDER_ID, orderId)
                        .eq(EntityProperty.feedback.USER_ID, userId));
    }

    /**
     * 房东回复反馈信息，并修改反馈状态
     */
    @Override
    public Integer updateReplyAndStatus(Long feedbackId, String reply, Integer status) {
        return feedbackDAO.updateReplyAndStatusById(feedbackId, reply, status);
    }
}
